package com.feature50.clarity.binding;

import com.feature50.util.ReflectionUtils;

import javax.beans.binding.Binding;
import javax.beans.binding.BindingContext;
import javax.swing.JTextField;
import java.util.ArrayList;
import java.util.List;

public class ClarityBindingContext extends BindingContext {
    private List<Binding> bindings = new ArrayList<Binding>();

    public void addBinding(Binding binding) {
        super.addBinding(binding);
        bindings.add(binding);
    }

    public void removeBinding(Binding binding) {
        super.removeBinding(binding);
        bindings.remove(bindings);
    }

    public void updateComponents() {
        for (int i = 0; i < bindings.size(); i++) {
            Binding binding = bindings.get(i);

            try {
                JTextField text = (JTextField) binding.getTarget();
                Object o = binding.getSource();
                text.setText((String) ReflectionUtils.invokeGetter(o, "first"));
            } catch (Exception e) {
                e.printStackTrace();
            }

            //binding.setSourceValueFromTargetValue();
        }
    }
}
